package cn.stylefeng.guns.modular.hxl.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.hxl.entity.HxlSortTask;
import cn.stylefeng.guns.modular.hxl.model.params.HxlAreaParam;
import cn.stylefeng.guns.modular.hxl.model.params.HxlSortTaskParam;
import cn.stylefeng.guns.modular.hxl.model.result.HxlAreaResult;
import cn.stylefeng.guns.modular.hxl.service.HxlAreaService;
import cn.stylefeng.guns.modular.hxl.service.HxlSortTaskService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


/**
 * 好想来-拣货任务控制器
 *
 * @author 
 * @Date 2025-06-30 09:18:16
 */
@Controller
@RequestMapping("/hxlSortTask")
public class HxlSortTaskController extends BaseController {

    private String PREFIX = "/hxl/hxlSortTask";

    @Autowired
    private HxlSortTaskService hxlSortTaskService;

    @Autowired
    private UserService userService;

    @Autowired
    private HxlAreaService hxlAreaService;

    /**
     * 跳转到主页面
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("")
    public String index(Model model) {
        HxlAreaParam param = new HxlAreaParam();
        List<HxlAreaResult> hxlAreaList = hxlAreaService.findListBySpec(param);
        model.addAttribute("hxlAreaList", hxlAreaList);
        return PREFIX + "/hxlSortTask.html";
    }

    /**
     * 新增页面
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/hxlSortTask_add.html";
    }

    /**
     * 编辑页面
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/hxlSortTask_edit.html";
    }

    /**
     * 新增接口
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(HxlSortTaskParam hxlSortTaskParam) {
        this.hxlSortTaskService.add(hxlSortTaskParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(HxlSortTaskParam hxlSortTaskParam) {
        this.hxlSortTaskService.update(hxlSortTaskParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(HxlSortTaskParam hxlSortTaskParam) {
        this.hxlSortTaskService.delete(hxlSortTaskParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author 
     * @Date 2025-06-30
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(HxlSortTaskParam hxlSortTaskParam) {
        HxlSortTask detail = this.hxlSortTaskService.getById(hxlSortTaskParam.getPickOrderNumber());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author 
     * @Date 2025-06-30
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(HxlSortTaskParam hxlSortTaskParam,String condition ) {
        return this.hxlSortTaskService.findPageBySpec(hxlSortTaskParam,condition);
    }


    /**
     * 跳转到导入页面
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel() {
        return PREFIX + "/import.html";
    }

    /**
     * 下载订单模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException {
        String path = "assets/excel/hxlSortTask.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("好想来拣货任务导入模板" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 快递明细
     * 上传excel
     */
    @RequestMapping(value = "/importExcel" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData importExcel(@RequestPart("file") MultipartFile file)  {
        try {
            return hxlSortTaskService.getExceldata(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseData.success();
    }

}


